package com.jeomo.masterdata.starter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.masterdata.starter.entity.Merchant;
import com.jeomo.masterdata.starter.entity.Tob;
import com.jeomo.masterdata.starter.mapper.MerchantMapper;
import com.jeomo.masterdata.starter.mapper.TobMapper;
import com.jeomo.masterdata.starter.service.ITobService;
import com.jeomo.masterdata.starter.util.CommonUtil;
import com.jeomo.shiro.util.LoginSessionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p></p>
 *
 * @author : 崔贺
 * @date : 2020-08-28 13:47
 **/
@Service
public class TobServiceImpl extends BaseServiceImpl<TobMapper, Tob> implements ITobService {
    @Resource
    private TobMapper tobMapper;
    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public int create(Tob tob) {
        String currentOrgCode = LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode();
        tob.setOrgCode(currentOrgCode);
        String recentlyCode = tobMapper.queryRecentlyCode(currentOrgCode);
        String code = CommonUtil.spliceCode(currentOrgCode, recentlyCode);
        tob.setCode(code);
        return tobMapper.insert(tob);
    }

    @Override
    public Boolean removeTob(String code) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tob_code", code);
        List<Merchant> merchants = merchantMapper.selectList(queryWrapper);
        if (merchants.isEmpty()) {
            tobMapper.deleteById(code);
            return true;
        } else {
            return false;
        }
    }
}
